package com.yc.jpyy.studentbase.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.base.util.PageBean;
import com.yc.jpyy.studentbase.dao.StudentBaseDao;
import com.yc.jpyy.studentbase.service.StudentBaseService;






@Service
public class StudentBaseServiceImpl extends BaseService implements StudentBaseService {
    
	@Autowired
	private StudentBaseDao studentBaseDao;

	

	@Override
	public int addStudentBaseService(Map<String, Object> paramsMap) throws DataAccessException {
		int ret = studentBaseDao.addStudentBaseDao(paramsMap);
		System.out.println(ret);
		return ret;
	}

	@Override
	public int updateStudentBaseService(Map<String, Object> paramsMap) throws DataAccessException {
		int ret = studentBaseDao.updateStudentBaseDao(paramsMap);
		return ret;
		
	}

	@Override
	public PageBean findStudentBaseService(Map<String, Object> paramsMap)
			throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		//HandlerDataSource.setDataSource("bizDataSource");
		List list = studentBaseDao.findStudentBaseDao(paramsMap);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findStudentBaseCountService(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public List<Map<String, Object>> findStudentBaseToDetilService(Map<String, Object> paramsMap)
			throws DataAccessException {
		return studentBaseDao.findStudentBaseToDetilDao(paramsMap);
	}

	

	@Override
	public List<Map<String, Object>> findExamResultService(Map<String, Object> paramsMap) throws DataAccessException {
		return studentBaseDao.findExamResultDao(paramsMap);
	}

	@Override
	public List<Map<String, Object>> findStudentAnswerTestService(Map<String, Object> paramsMap)
			throws DataAccessException {
		return studentBaseDao.findStudentAnswerTestDao(paramsMap);
	}

	@Override
	public PageBean findStudentBaseToProfessionService(Map<String, Object> paramsMap)
			throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		//HandlerDataSource.setDataSource("bizDataSource");
		List<Map<String, Object>> list = studentBaseDao.findStudentBaseToProfessionDao(paramsMap);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findStudentBaseToProfessionCountService(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public int updateStudentBaseExamStatusService(Map<String, Object> paramsMap) throws DataAccessException {
		return studentBaseDao.updateStudentBaseExamStatusDao(paramsMap);
	}

	@Override
	public int findStudentBaseCountService(Map<String, Object> paramsMap) throws DataAccessException {
		return studentBaseDao.findStudentBaseCountDao(paramsMap);
	}

	@Override
	public PageBean findStudentBaseToExamResultService(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		//HandlerDataSource.setDataSource("bizDataSource");
		List list = studentBaseDao.findStudentBaseToExamResultDao(paramsMap);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findStudentBaseToExamResultCountService(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public int findStudentBaseToExamResultCountService(Map<String, Object> paramsMap) throws DataAccessException {
		return studentBaseDao.findStudentBaseToExamResultCountDao(paramsMap);
	}

	@Override
	public int findStudentBaseToProfessionCountService(Map<String, Object> paramsMap) throws DataAccessException {
		return studentBaseDao.findStudentBaseToProfessionCountDao(paramsMap);
	}

	
	
	

	

	@Override
	public PageBean findStudentBaseToProfessionExamService(Map<String, Object> paramsMap)
			throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		//HandlerDataSource.setDataSource("bizDataSource");
		List list = studentBaseDao.findStudentBaseToProfessionDao(paramsMap);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findStudentBaseToProfessionCountService(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	
	@Override
	public int findStudentBaseToProfessionExamCountService(Map<String, Object> paramsMap) throws DataAccessException {
		return studentBaseDao.findStudentBaseToProfessionCountDao(paramsMap);
	}

	@Override
	public Map<String, Object> addRotationTraining(Map<String, Object> paramsMap)
			throws Exception {
		Map<String,Object> resultMap = new HashMap<>();
		//根据行业ID查询科目Id
		List<String> list = studentBaseDao.selectSubjectidsByProfessinid(paramsMap);
		String subjectids = StringUtils.join(list, ";");// 以逗号分割
		paramsMap.put("subjectids", subjectids);
		paramsMap.put("subjectidlist", subjectids);
		//ISSGZ=空 ，ISLUNXUN=空 新增学员表;ISSGZ=1，ISLUNXUN=空  根据Guid更新学员表;ISSGZ=空，ISLUNXUN=1  根据Idcard更新学员表;		
		String issgz = Optional.ofNullable(paramsMap.get("issgz")).orElse("").toString();
		String islunxun = Optional.ofNullable(paramsMap.get("islunxun")).orElse("").toString();
		if(issgz.equals("") && islunxun.equals("")) {
			//新增
			int j = studentBaseDao.addStudentBaseRotation(paramsMap);
			if(j > 0){
				resultMap.put("returnCode", 1);
				resultMap.put("returnMsg", "操作成功");
			}else{
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "操作失败");
			}
		}else if(issgz.equals("1") && islunxun.equals("")){
			//根据Guid更新学员表
			int i = studentBaseDao.updateStudentBaseRotation(paramsMap);
			if(i > 0){
				resultMap.put("returnCode", 1);
				resultMap.put("returnMsg", "操作成功");
			}else{
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "操作失败");
			}
		}else if(issgz.equals("") && islunxun.equals("1")){
			//根据Idcard更新学员表
			int k = studentBaseDao.updateStuRotationByIdCard(paramsMap);
			if(k > 0){
				resultMap.put("returnCode", 1);
				resultMap.put("returnMsg", "操作成功");
			}else{
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "操作失败");
			}
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> checkIsHaveStudent(Map<String, Object> paramsMap)
			throws Exception {
		Map<String,Object> resultMap = new HashMap<>();
		int i = studentBaseDao.checkIsHaveStudent(paramsMap);
		if(i > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "该学员已存在");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "该学员不存在");
		}
		return resultMap;
	}

	
}
